# Read the data file and run analyses.

setwd("F:/Experiment Data/Complex Contagion/ComplexContagionDatasets/")
exp3 = read.csv("Exp3_polarizing_no_keep_five_neighbor.csv")

# Sanity checks.
head(exp3)
dim(exp3)
table(exp3$finished)
table(exp3$conditionName)

# Code dependent variables.
# Code Self, Schis and Other columns.
exp3$Self = 0
exp3[exp3$userChoice == "Keep_10_cents_and_Do_Not_Donate", c("Self")] = 1
exp3$Schis = 0
exp3[exp3$userChoice == "Schistosomiasis_Control_Initiative", c("Schis")] = 1
exp3$Other = 0
exp3[exp3$Self == 0 & exp3$Schis == 0, c("Other")] = 1

# Code AnyCharity column.
exp3$AnyCharity = 0
exp3[exp3$userChoice != "Keep_10_cents_and_Do_Not_Donate", c("AnyCharity")] = 1

# Code independent variables.
# Code Tie Strength column.
exp3$TieStrength = "High"
exp3[grepl("^AllLo__", exp3$conditionName), c("TieStrength")] = "Low"
#exp3[, c("conditionName", "TieStrength")]

# Code SelfNeighbors, SchisNeighbors and OtherNeighbors columns.
exp3$SelfNeighbors = 0
exp3[grepl("Self_1", exp3$conditionName), c("SelfNeighbors")] = 1
exp3[grepl("Self_2", exp3$conditionName), c("SelfNeighbors")] = 2
exp3[grepl("Self_3", exp3$conditionName), c("SelfNeighbors")] = 3

exp3$SchisNeighbors = 0
exp3[grepl("Schis_1", exp3$conditionName), c("SchisNeighbors")] = 1
exp3[grepl("Schis_2", exp3$conditionName), c("SchisNeighbors")] = 2
exp3[grepl("Schis_3", exp3$conditionName), c("SchisNeighbors")] = 3

exp3$OtherNeighbors = 0
exp3[grepl("Rand_1", exp3$conditionName), c("OtherNeighbors")] = 1
exp3[grepl("Rand_2", exp3$conditionName), c("OtherNeighbors")] = 2
exp3[grepl("Rand_3", exp3$conditionName), c("OtherNeighbors")] = 3

exp3[, c("conditionName", "TieStrength", "SelfNeighbors", "SchisNeighbors", "OtherNeighbors")]


# Influence curves.  First split by tie strength, then together.
# UserChoice is Schis by Schis neighbor count.  Split by Tie Strength.
aggregate(Schis ~ TieStrength + SchisNeighbors, exp3, mean)
aggregate(Schis ~ SchisNeighbors, exp3, mean)

# Means will give us the proportions, but let's also get the raw numerator and denominator.
exp3$countMe = 1
aggregate(Schis ~ TieStrength + SchisNeighbors, exp3, sum)
aggregate(countMe ~ TieStrength + SchisNeighbors, exp3, sum)
table(exp3$SchisNeighbors)


# Test linear versus exponential fit for the Schis outcome as function of neighbor count.
schisDataToFit = as.data.frame(aggregate(Schis ~ SchisNeighbors, exp3, mean))
linearModel = lm(Schis ~ SchisNeighbors, data=schisDataToFit)
summary(linearModel)
expModel = lm(log(Schis) ~ SchisNeighbors, data=schisDataToFit)
summary(expModel)

# Compare models on plot.
png("exp3_model_comparison.png")
plot(schisDataToFit$SchisNeighbors, schisDataToFit$Schis, pch=16, xlab = "SchisNeighbors", ylab = "Percent Choosing Schis")
xValues = seq(0, 3, 0.1)
expModelPredictions = exp(predict(expModel, list(SchisNeighbors=xValues)))
lines(xValues, expModelPredictions, lwd=2, col = "red")
linearModelPredictions = predict(linearModel, list(SchisNeighbors=xValues))
lines(xValues, linearModelPredictions, lwd=2, col = "blue", lty=5)
legend("topleft", inset=0.05, legend=c("Linear Fit", "Exponential Fit"), col=c("blue", "red"), lwd=c(2,2), lty=c("longdash", "solid"))
dev.off()

# Compare nested models using ANOVA.
linearModel = lm(Schis ~ SchisNeighbors, data=schisDataToFit)
linearModelWithZeroIndicator = lm(Schis ~ SchisNeighbors + I(SchisNeighbors==0), data=schisDataToFit)
summary(linearModelWithZeroIndicator)
anova(linearModel, linearModelWithZeroIndicator, test="F")

linearModel = lm(Schis ~ SchisNeighbors, data=schisDataToFit)
linearModelWithExpAlterCounts = lm(Schis ~ SchisNeighbors + I(exp(SchisNeighbors)), data=schisDataToFit)
summary(linearModelWithExpAlterCounts)
anova(linearModel, linearModelWithExpAlterCounts, test="F")


# Create influence curve per tie strength plot.
infCurveByTieStrengthNumbers = aggregate(Schis ~ TieStrength + SchisNeighbors, exp3, mean)
highPoints = infCurveByTieStrengthNumbers[infCurveByTieStrengthNumbers$TieStrength == "High", c("SchisNeighbors", "Schis")]
lowPoints = infCurveByTieStrengthNumbers[infCurveByTieStrengthNumbers$TieStrength == "Low", c("SchisNeighbors", "Schis")]
png("exp3_inf_line_by_tie_strength.png")
plot(highPoints$SchisNeighbors, highPoints$Schis, pch=16, col="#17202a", cex=1.25, xlab = "SCI Neighbors", ylab = "Proportion Choosing SCI", ylim=c(0, 0.15))
points(lowPoints$SchisNeighbors, lowPoints$Schis, pch=17, col="#99a3a4", cex=1.25)
# Plot a linear fit to the High points.
highFit = lm(Schis ~ SchisNeighbors, data=highPoints)
xValues = seq(0, 3, 0.1)
highFitPredictions = predict(highFit, list(SchisNeighbors=xValues))
lines(xValues, highFitPredictions, lwd=2, lty="solid", col="#17202a")
# Plot a linear fit to the Low points.
lowFit = lm(Schis ~ SchisNeighbors, data=lowPoints)
xValues = seq(0, 3, 0.1)
lowFitPredictions = predict(lowFit, list(SchisNeighbors=xValues))
lines(xValues, lowFitPredictions, lwd=2, lty="dashed", col="#99a3a4")
legend("topleft", inset=0.05, legend=c("High Tie Strength", "Low Tie Strength"), pch=c(16, 17), col=c("#17202a", "#99a3a4"), lwd=c(2,2), lty=c("solid", "dashed"))
dev.off()

